package com.example.a10.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class DocumentUtil {

    /**
     * 计算文件大小
     * @return
     */
    public static String getFilesize(String filePath){
        File file1 = new File(filePath);
        long length = file1.length();
        //转化为MB
        double fileSizeInKB = (double) length / (1024);
        if(fileSizeInKB>1000){
            double v = (double) length / (1024*1024);
            String formattedNumber = String.format("%.2f", v)+"MB";
            return formattedNumber;
        }
        String formattedNumber = String.format("%.2f", fileSizeInKB)+"KB";
        return formattedNumber;
    }

    public static String getFileSize(long length){
        //转化为MB
        double fileSizeInKB = (double) length / (1024);
        if(fileSizeInKB>1000){
            double v = (double) length / (1024*1024);
            String formattedNumber = String.format("%.2f", v)+"MB";
            return formattedNumber;
        }
        String formattedNumber = String.format("%.2f", fileSizeInKB)+"KB";
        return formattedNumber;
    }

}
